/*
$handle = {
	doGet	: doHttpGet,
	doPost	: doHttpPost,
	doHandle: doHandle,
	getHttpHandle : getHttpHandle,
	getDomHandle  : getDomHandle,
	getContextBase: getContextBase
}

//$Logger.show();
*/
//if not run in server, only as local file system, using this.
function doLoadXml(surl) {
   	var objReturn = {};
		objReturn.debug += "succeed in get xml doc. \n";
		objReturn.doc = getDomHandle();
		objReturn.doc.load(surl);
	return objReturn;	
}

function isRemoteRequest(surl) {
	var isRemote = false;
	if(surl&&surl.substr(0,4).toUpperCase()=="HTTP"){//absolute path;
		isRemote = true;
	}else if(document&&document.location){//ralative path;
		if(document.location.protocol.substr(0,4).toUpperCase()=="HTTP"){
			isRemote = true;
		}
	}
	return isRemote;
}

function doHttpGet(surl,sdoc) {
	return doHttpPost(surl,sdoc,"GET");	
}

function doHttpPost(surl,sdoc,method,isForm,async) {
	if(!isRemoteRequest(surl)){
		return doLoadXml(surl);
	}
	var qIndex = surl.indexOf("?");
	if((!sdoc) && qIndex>0){
		//if no doc, we can use post form for pass params, especially for no roma letter.
		method="POST";
		isForm=true;
		sdoc=surl.substr(qIndex+1);
		surl=surl.substr(0,qIndex);
	}
	
   	var objReturn = {};
	var oCacKey = "cachedHttpHandleObject";
	var oLocked = new Object();
     var xmlHttpHandle = getCachedObject(oCacKey,getHttpHandle,oLocked).object;
    if (!xmlHttpHandle) {
		objReturn.debug ="不能创建XMLHTTP对象!";
		releaseCachedObject(oCacKey,oLocked)
        return objReturn;
    }
    xmlHttpHandle.abort();
    xmlHttpHandle.onreadystatechange = fnBind(doHttpTransport,null,xmlHttpHandle,objReturn);

    xmlHttpHandle.open(method?method:"POST",encodeURI(surl),async?true:false);
    //if post form data, please use/take care encodeURI(postdata);
    if(isForm){
        //xmlHttpHandle.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        xmlHttpHandle.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    }
    xmlHttpHandle.send(sdoc?sdoc:null);
    //this.logger.debug(objReturn.text);
    //this.logger.debug(objReturn.xml);
	releaseCachedObject(oCacKey,oLocked)
    return objReturn;
}
	
function doHttpTransport(httpHandle,objReturn){
	objReturn.debug = "httpHandle.readyState = "+httpHandle.readyState+"\n";
	if(httpHandle.readyState==4){
		if(httpHandle.status==200){
		    //this.logger.debug("通讯完成");
		    //alert(httpHandle.responseText);
		    objReturn.debug = "ResponseHeaders:\n"+httpHandle.getAllResponseHeaders()+"\n";
			if(httpHandle.responseText==""){
				objReturn.debug += "responseText : Empty! \n";
				//this.logger.debug("服务器无返回消息,操作可能失败!");
			}else if(httpHandle.responseXML.xml!=""){
				objReturn.debug += "succeed in get xml doc. \n";
			    objReturn.doc = getDomHandle();
			    objReturn.doc.loadXML(httpHandle.responseXML.xml);
				//objReturn.debug += objReturn.doc.documentElement.xml;
				//alert(objReturn.debug);
			}else{
				objReturn.debug += "can't get xml doc, please get responseText.\n";
				objReturn.debug += httpHandle.responseXML.parseError.reason+"\n";
				objReturn.debug += httpHandle.responseXML.parseError.srcText;
				objReturn.text=httpHandle.responseText;
				//objReturn.debug += objReturn.text;
			}
		}else{
			objReturn.debug = "httpHandle.status = "+httpHandle.status+": "+httpHandle.statusText+".\n";
		    objReturn.debug+= "ResponseHeaders:\n"+httpHandle.getAllResponseHeaders()+"\n";
		}
	}
}

